package com.automation.ljf.controller;

import com.automation.ljf.domain.request.CaseRequest;
import com.automation.ljf.domain.request.CaseStepRequest;
import com.automation.ljf.service.TestUseCaseService;
import com.automation.ljf.util.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 用户基础操作接口
 *
 * @author lijiefeng
 * @since 2021/8/23
 */
@Api(value = "测试用例基础操作接口", tags = {"测试用例基础操作接口"})
@RestController
@CrossOrigin
public class UserBasicsController {

    @Autowired
    private TestUseCaseService userBasicsService;

    @ApiOperation(value = "创建测试用例", tags = {"创建测试用例"})
    @PutMapping(value = "/mf/case")
    public ResultMessage createTestCase(@ApiParam("测试用例数据传输") @RequestBody CaseRequest caseRequest){

        return userBasicsService.createCase(caseRequest);

    }

    @ApiOperation(value = "创建用例步骤", tags = {"添加用例步骤"})
    @PutMapping(value = "/mf/cases/steps")
    public ResultMessage createCaseStep(@ApiParam("测试用例步骤数组") @RequestBody CaseStepRequest caseStepRequest){

        return userBasicsService.createCaseStep(caseStepRequest);

    }

    @ApiOperation(value = "查询用户创建的所有测试用例", tags = {"查询用户创建的所有测试用例"})
    @GetMapping(value = "/mf/user/{admUserId}/cases")
    public ResultMessage selectCaseByUId(@ApiParam("用户id") @PathVariable("admUserId") String admUserId,
                                        @ApiParam("当前页码") @RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
                                        @ApiParam("每页的数量") @RequestParam(value = "limit",defaultValue = "10", required = false) Integer limit){

        return userBasicsService.selectCaseByUId(admUserId, page, limit);

    }

    @ApiOperation(value = "查询测试用例详情", tags = {"查询测试用例详情"})
    @GetMapping(value = "/mf/cases/{caseId}")
    public ResultMessage selectCaseByCaseId(@ApiParam("测试用例id") @PathVariable("caseId") String caseId){

        return userBasicsService.selectCaseByCaseId(caseId);

    }

    @ApiOperation(value = "执行测试用例", tags = {"执行测试用例"})
    @PostMapping(value = "/mf/cases/{caseId}")
    public ResultMessage ExecuteCaseByCaseId(@ApiParam("测试用例id") @PathVariable("caseId") String caseId,
                                              @ApiParam("接收JSON") @RequestBody(required = false) String jsonData){

        return userBasicsService.ExecuteCaseByCaseId(caseId, jsonData);

    }

    @ApiOperation(value = "查询用例的所有步骤", tags = {"查询用例的所有步骤"})
    @GetMapping(value = "/mf/cases/{caseId}/steps")
    public ResultMessage selectStepByCaseId(@ApiParam("用例id") @PathVariable String caseId,
                                            @ApiParam("当前页码") @RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
                                            @ApiParam("每页的数量") @RequestParam(value = "limit",defaultValue = "8", required = false) Integer limit){
        return userBasicsService.selectStepByCaseId(caseId, page, limit);
    }

    @ApiOperation(value = "查询用例是否存在需要输入的步骤", tags = {"查询用例是否存在需要输入的步骤"})
    @GetMapping(value = "/mf/cases/{caseId}/size")
    public ResultMessage selectStepIsInputByCaseId(@ApiParam("用例id") @PathVariable("caseId") String caseId){
        return userBasicsService.selectStepIsInputByCaseId(caseId);
    }

    @ApiOperation(value = "查询用例结果集", tags = {"查询用例结果集"})
    @GetMapping(value = "/mf/user/{uId}/case-report")
    public ResultMessage selectCaseReportByUId(@ApiParam("用户id") @PathVariable String uId,
                                               @ApiParam("当前页码") @RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
                                               @ApiParam("每页的数量") @RequestParam(value = "limit",defaultValue = "8", required = false) Integer limit){
        return userBasicsService.selectCaseReportByUId(uId, page, limit);
    }

    @ApiOperation(value = "根据用例结果集的记录id查询用例步骤结果", tags = {"根据用例结果集的记录id查询用例步骤结果"})
    @GetMapping(value = "/mf/case-report/{reportId}/step-report")
    public ResultMessage selectStepReportByCaseReportId(@ApiParam("记录id") @PathVariable String reportId,
                                                        @ApiParam("当前页码") @RequestParam(value = "page", defaultValue = "0", required = false) Integer page,
                                                        @ApiParam("每页的数量") @RequestParam(value = "limit",defaultValue = "8", required = false) Integer limit){
        return userBasicsService.selectStepReportByCaseReportId(reportId, page, limit);

    }

    @ApiOperation(value = "修改测试用例信息", tags = {"修改测试用例信息"})
    @PutMapping(value = "/mf/cases")
    public ResultMessage updateCaseByCaseId(@ApiParam("用例实体") @RequestBody CaseRequest caseRequest){
        return userBasicsService.updateCaseByCaseId(caseRequest);
    }


    @ApiOperation(value = "批量删除测试用例", tags = {"批量删除测试用例"})
    @DeleteMapping(value = "/mf/cases")
    public ResultMessage deleteCaseByCaseId(@ApiParam("用例实体数组") @RequestBody List<CaseRequest> caseRequestList){
        return userBasicsService.deleteCaseByCaseId(caseRequestList);
    }

    @ApiOperation(value = "批量删除用例步骤", tags = {"批量删除用例步骤"})
    @DeleteMapping(value = "/mf/cases/{caseId}/steps")
    public ResultMessage deleteStepByCaseId(@ApiParam("用例编号") @PathVariable String caseId,
                                            @ApiParam("步骤实体数组") @RequestBody List<CaseStepRequest> caseStepRequests){
        return userBasicsService.deleteStepByCaseId(caseId, caseStepRequests);
    }

}

